
package com.csc440.team2.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Display;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout.LayoutParams;


import com.csc440.team2.SmilConstants;
import com.csc440.team2.layout.MessageLayout;
import com.csc440.team2.layout.MessageLayoutRegion;
import com.csc440.team2.smil.SMILMessage;


/**
 * Activity acting as a container for the split layout builder
 * 
 * @author Jeremy Spitzig, Brad Gardner, Santiago Jamriska, Alex Povkov
 * 
 */
public class CreateSMILLayout extends Activity implements RegionClickedHandler
{
    private SMILMessage message;
    boolean newMessage = false;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        final Bundle bundle = getIntent().getExtras();

        if (bundle != null)
        {
            message = (SMILMessage) bundle.getSerializable(SmilConstants.MESSAGE);
            int componentCount = message.getComponentList().size();

            /*
             * if no components are on the message, treat it as a new message.
             */
            if (componentCount == 0)
                newMessage = true;

            buildLayout();
        }
        else
        {
            finish();
        }
    }


    /**
     * Builds the primary layout for the activity
     */
    public void buildLayout()
    {
        MessageLayout layout;
        Display display = getWindowManager().getDefaultDisplay();
        LayoutParams layoutParams = new LayoutParams(display.getWidth(), display.getHeight());

        layout = new MessageLayout(this, this);
        layout.setLayoutParams(layoutParams);
        setContentView(layout);

        layout.buildLayout(message);
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        MenuItem newItem;
        menu.setHeaderTitle("Layout");
        newItem = menu.add("Split Horizontally");
        newItem.setOnMenuItemClickListener(((MessageLayoutRegion) v));
        newItem = menu.add("Split Vertically");
        newItem.setOnMenuItemClickListener(((MessageLayoutRegion) v));
        newItem = menu.add("Cancel Split");
        newItem = menu.add("Cancel Message");
        newItem = menu.add("Finish");
    }


    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        if (item.getTitle().equals("Cancel Message"))
        {
            finish();
        }
        else if (item.getTitle().equals("Finish"))
        {
            /*
             * If this was a new message, forward to the components intent,
             * otherwise we must be editing an existing message, so return to
             * the caller
             */
            if (newMessage)
            {
                Intent myIntent = new Intent(this, Components.class);
                myIntent.putExtra(SmilConstants.MESSAGE, message);
                startActivity(myIntent);
            }
            else
            {
                returnToParent();
            }
        }
        return true;
    }


    @Override
    public void regionClicked(View v)
    {
        String regionId = ((MessageLayoutRegion) v).getRegionId();

        Intent in = new Intent(CreateSMILLayout.this, AddComponents.class);
        in.putExtra(SmilConstants.MESSAGE, message);
        in.putExtra(SmilConstants.REGION_ID, regionId);
        CreateSMILLayout.this.startActivityForResult(in, 1);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        /*
         * get the modified message and rebuild the layout with its contents
         */
        if (data != null)
        {
            message = (SMILMessage) data.getExtras().get(SmilConstants.MESSAGE);

            Log.i("CreateSMILLayout", "activity returned");

            buildLayout();
        }
    }


    /**
     * Packages the message into an intent and returns to the calling activity
     */
    public void returnToParent()
    {
        Intent data = new Intent();

        data.putExtra(SmilConstants.MESSAGE, message);
        setResult(1, data);
        finish();
    }
}
